<template>
   <a-form layout="vertical" :model="modelValue" ref="formRef" :rules="rules">
        <a-form-item label="企业名称" field="businessName">
            <a-input placeholder="请输入" v-model="modelValue.businessName"></a-input>
        </a-form-item>
        <a-form-item label="企业简称" field="businessShortName">
            <a-input placeholder="请输入" v-model="modelValue.businessShortName"></a-input>
        </a-form-item>
        <a-form-item label="社会信用代码" field="unifiedSocialCreditCode">
            <a-input placeholder="请输入" v-model="modelValue.unifiedSocialCreditCode"></a-input>
        </a-form-item>
        <a-form-item label="经营者名称" field="operatorName">
            <a-input placeholder="请输入" v-model="modelValue.operatorName"></a-input>
        </a-form-item>
        <a-form-item label="经营者身份证号" field="idNumber">
            <a-input placeholder="请输入" v-model="modelValue.idNumber"></a-input>
        </a-form-item>
        <a-form-item label="经营者手机号" field="mobileNumber">
            <a-input placeholder="请输入" v-model="modelValue.mobileNumber"></a-input>
        </a-form-item>
        <a-form-item label="经营者身份证" field="idCardFrontImage">
            <IdCardUpload v-model:frontUrl="modelValue.idCardFrontImage" v-model:backUrl="modelValue.idCardBackImage" />
        </a-form-item>
        <a-form-item label="身份证开始日期" field="idCardValidStartDate">
            <a-date-picker style="flex:1" v-model="modelValue.idCardValidStartDate" value-format="YYYY-MM-DD"
                placeholder="请选择" />
        </a-form-item>
        <a-form-item label="身份证结束日期" field="idCardValidEndDate">
            <EndTime v-model="modelValue.idCardValidEndDate" />
        </a-form-item>
        <a-form-item label="营业执照" field="businessLicenseImage">
            <div class="upload-box">
                <div>
                    <a-upload action="/" :show-file-list="false" :custom-request="uploadLicense">
                        <template #upload-button>
                            <div class="upload-ele" v-if="!modelValue.businessLicenseImage">
                                <icon-plus />
                                点击上传
                            </div>
                            <div v-else class="upload-img-preview">
                                <img :src="modelValue.businessLicenseImage" />
                            </div>
                        </template>
                    </a-upload>
                </div>
            </div>
        </a-form-item>
        <a-form-item label="注册日期" field="registrationDate">
            <a-date-picker style="flex:1" v-model="modelValue.registrationDate" value-format="YYYY-MM-DD"
                placeholder="请选择" />
        </a-form-item>
        <a-form-item label="营业执照开始日期" field="businessLicenseValidStartDate">
            <a-date-picker style="flex:1" v-model="modelValue.businessLicenseValidStartDate" value-format="YYYY-MM-DD"
                placeholder="请选择" />
        </a-form-item>
        <a-form-item label="营业执照结束日期" field="businessLicenseValidEndDate">
            <EndTime v-model="modelValue.businessLicenseValidEndDate" />
        </a-form-item>
    </a-form>
</template>

<script setup lang="ts">
import { ref, watch } from "vue";
import IdCardUpload from "./IdCardUpload.vue";
import { identityCard, mobile, code, required, creditCode, length } from "../rules";
import upload, { isLegitimate } from "../upload";
import EndTime from "./EndTime.vue";
import { Message } from "@arco-design/web-vue";
import { compressorImage } from "@/utils/compressorFn";
const props = defineProps({
    modelValue: {
        type: Object,
        required: true
    }
})
const emit = defineEmits(["update:modelValue"]);
/**
 * 校验图片格式以及大小
 */
 const validateImage = (file:File) => {
    const type = ['image/jpg', 'image/jpeg', 'image/png'];
    const legitimate = isLegitimate(file, type, 500);
    if (legitimate.msg) {
        Message.error(legitimate.msg);
        return false;
    } else {
        return true;
    }
}
/**
 * 身份证正反面校验
 */
const verifyIdentityCardImage = (value:string, callback:Function) => {
    const condition = (props.modelValue.idCardBackImage && props.modelValue.idCardFrontImage);
    if (condition) {
        return callback()
    } else if (!props.modelValue.idCardFrontImage) {
        return callback('请上传身份证正面')
    } else if (!props.modelValue.idCardBackImage) {
        return callback('请上传身份证反面')
    }
}
/**
 * 注册企业信息表单校验规则
 */
const rules = {
    businessName: [
        required('请输入完整的企业名称'),
        length(2, 50, '企业名称')
    ],
    operatorName: [
        required('请输入经营者名称'),
        length(2, 50, '经营者名称')
    ],
    unifiedSocialCreditCode: creditCode,
    mobileNumber: [
        mobile,
        required('请输入手机号'),
    ],
    verificationCode:[
        code(4),
        required('请输入验证码'),
    ],
    businessLicenseImage: required('请上传营业执照'),
    idCardValidStartDate: required('请选择身份证有效期开始时间'),
    idCardValidEndDate: required('请选择身份证有效期结束时间'),
    businessLicenseValidStartDate: required('请选择营业执照有效期开始时间'),
    businessLicenseValidEndDate: required('请选择营业执照有效期结束时间'),
    businessScope: [
        required('请输入经营范围'),
        length(1, 32, '经营范围')
    ],
    enterpriseShortName: [
        required('请输入简称'),
        length(1, 32, '简称')
    ],
    registeredCapital: [
        required('请输入注册资本'),
        { match: /^[0-9]+(\.[0-9]{1,2})?$/, message: '请输入正确的注册资本', trigger: 'blur' }
    ],
    idCardFrontImage: [
        required('请上传身份证'),
        { validator: verifyIdentityCardImage, trigger: 'change' }
    ],
    enterpriseEmail: [
        required('请输入邮箱'),
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
    ],
    enterpriseAddress: [
        required('请输入企业地址'),
        length(1, 100, '企业地址')
    ],
    businessShortName: [
        required('请输入企业简称'),
        length(1, 32, '企业简称')
    ],
    idNumber: identityCard,
    registrationDate: required('请选择注册日期')
}
watch(() => props.modelValue.idCardFrontImage, () => {
    formRef.value.validateField(['idCardFrontImage'], () => { })
})
watch(() => props.modelValue.idCardBackImage, () => {
    formRef.value.validateField(['idCardFrontImage'], () => { })
})
/**
 * 营业执照上传
 */
const uploadLicense = async ({fileItem}:any) => {
  compressorImage(fileItem, async () => {
    if (validateImage(fileItem.file)) {
      const fileInfo = await upload(fileItem.file);
      props.modelValue.businessLicenseImage = fileInfo.url;
    }
  });

}
const formRef = ref()
const validate = () => {
    return new Promise((resolve) => {
        formRef.value.validate((error:any) => {
            resolve(error==undefined)
        })
    })
}
const resetFields = () => {
    return formRef.value.resetFields()
}
defineExpose({
    validate,
    resetFields
})
</script>

<style lang="scss" scoped>
.upload-box {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 30px;
    flex: 1;

    >div {
        width: 100%;
        text-align: center;
        color: #3D3D3D;
    }

    .upload-ele {
        background: #F2F3F5;
        height: 144px;
        margin-bottom: 10px;
        padding-top: 40px;
        box-sizing: border-box;
    }

    .upload-img-preview {
        height: 144px;

        img {
            height: 100%;
            width: 100%;
        }
    }

    ::v-deep(.arco-upload) {
        display: block;
        color: #C9CDD4;
        text-align: center;

        .arco-icon {
            display: block;
            margin: 0 auto 10px;
            font-size: 30px;
        }
    }
}
</style>